package tool

import play.api.data.Forms._
import play.api.data.JodaForms.jodaDate
import play.api.data._
import shared.Pojo._
import tool.Pojo.{_}

/**
 * Created by yz on 2018/7/17
 */

class FormTool {

  case class AccountData(account: String, password: String)

  val accountForm = Form(
    mapping(
      "account" -> text,
      "password" -> text
    )(AccountData.apply)(AccountData.unapply)
  )

  val configNameForm = Form(
    mapping(
      "configName" -> text
    )(ConfigNameData.apply)(ConfigNameData.unapply)
  )

  val idOpForm = Form(
    mapping(
      "id" -> optional(number)
    )(IdOpData.apply)(IdOpData.unapply)
  )

  val saveConfigForm = Form(
    mapping(
      "missionName" -> text,
      "configName" -> text
    )(ConfigFormData.apply)(ConfigFormData.unapply)
  )

  val userForm = Form(
    mapping(
      "name" -> text,
      "password" -> text
    )(UserData.apply)(UserData.unapply)
  )

  val unitForm = Form(
    mapping(
      "unit" -> text
    )(UnitData.apply)(UnitData.unapply)
  )

  case class IdData(id: Int)

  val idForm = Form(
    mapping(
      "id" -> number
    )(IdData.apply)(IdData.unapply)
  )

  val sampleIdForm = Form(
    mapping(
      "id" -> text
    )(SampleIdData.apply)(SampleIdData.unapply)
  )

  case class MissionNameData(missionName: String)

  val missionNameForm = Form(
    mapping(
      "missionName" -> text
    )(MissionNameData.apply)(MissionNameData.unapply)
  )

  case class AdjustMissionData(mission: String)

  val adjustMissionForm = Form(
    mapping(
      "mission" -> text,
    )(AdjustMissionData.apply)(AdjustMissionData.unapply)
  )

  case class MissionIdOptionData(missionId: Option[Int])

  val missionIdOptionForm = Form(
    mapping(
      "missionId" -> optional(number)
    )(MissionIdOptionData.apply)(MissionIdOptionData.unapply)
  )

  case class ArgsData(missionId: Int, compoundName: String)

  val argsForm = Form(
    mapping(
      "missionId" -> number,
      "compoundName" -> text,
    )(ArgsData.apply)(ArgsData.unapply)
  )

  val strForm = Form(
    mapping(
      "strSiteNames" -> seq(text),
      "strSiteDatas" -> seq(text)
    )(StrData.apply)(StrData.unapply)
  )

  val snpForm = Form(
    mapping(
      "snpSiteNames" -> seq(text),
      "snpSiteDatas" -> seq(text)
    )(SnpData.apply)(SnpData.unapply)
  )

  case class ParamAdjustData(missionName: String, missionId: Int, compoundName: String, flMin: String, flMax: String, step: String, nups: String,
                             ndowns: String, snr: String, iteration: String, bLine: String, rtlw: String, rtrw: String,
                             rt: String, peakLocation: String)

  val paramAdjustForm = Form(
    mapping(
      "missionName" -> text,
      "missionId" -> number,
      "compoundName" -> text,
      "flMin" -> text,
      "flMax" -> text,
      "step" -> text,
      "nups" -> text,
      "ndowns" -> text,
      "snr" -> text,
      "iteration" -> text,
      "bLine" -> text,
      "rtlw" -> text,
      "rtrw" -> text,
      "rt" -> text,
      "peakLocation" -> text
    )(ParamAdjustData.apply)(ParamAdjustData.unapply)
  )

  case class UnitConversionData(unit: String, toUnit: String, fromC: Int)

  val unitConversionForm = Form(
    mapping(
      "unit" -> text,
      "toUnit" -> text,
      "fromC" -> number
    )(UnitConversionData.apply)(UnitConversionData.unapply)
  )

  case class FileNameData(fileName: String)

  val fileNameForm = Form(
    mapping(
      "fileName" -> text
    )(FileNameData.apply)(FileNameData.unapply)
  )

  case class FileNamesData(fileNames: Seq[String])

  val fileNamesForm = Form(
    mapping(
      "fileNames" -> seq(text)
    )(FileNamesData.apply)(FileNamesData.unapply)
  )

}

object FormTool {

  val userNameForm = Form(
    mapping(
      "name" -> text
    )(UserNameData.apply)(UserNameData.unapply)
  )

  val missionNameCheckForm = Form(
    mapping(
      "missionName" -> text,
      "kind" -> text,
    )(MissionNameCheckData.apply)(MissionNameCheckData.unapply)
  )

  val searchForm = Form(
    mapping(
      "missionName" -> optional(text),
      "missionIntro" -> optional(text),
      "startDate" -> optional(jodaDate),
      "endDate" -> optional(jodaDate)
    )(SearchData.apply)(SearchData.unapply)
  )

  val sampleSearchForm = Form(
    mapping(
      "id" -> text,
      "indexId" -> text,
      "sampleIntro" -> text,
      "gender" -> text,
      "kind" -> text,
      "comment" -> text,
      "updateMission" -> text,
      "startDate" -> optional(jodaDate),
      "endDate" -> optional(jodaDate)
    )(SampleSearchData.apply)(SampleSearchData.unapply)
  )

  val missionForm = Form(
    mapping(
      "missionName" -> text,
      "lane" -> text,
      "id" -> text,
      "gender" -> text,
      "kind" -> text
    )(MissionData.apply)(MissionData.unapply)
  )

  val long2fatForm = Form(
    mapping(
      "missionName" -> text,
      "inputKind" -> text,
      "fileContent" -> text
    )(Long2fatData.apply)(Long2fatData.unapply)
  )

  val trainForm = {
    "" -> mapping(
      "useTrain" -> boolean,
      "trainPercent" -> text,
      "seed" -> text,
    )(TrainData.apply)(TrainData.unapply)
  }

  val preDealForm = Form(
    mapping(
      "missionName" -> text,
      "file" -> text,
      "coef" -> text,
      "replaceMethod" -> text,
      "knn" -> text,
      "min" -> text,
      "isNormal" -> text
    )(PreDealData.apply)(PreDealData.unapply)
  )

  val annoForm = Form(
    mapping(
      "missionName" -> text,
      "file" -> text,
    )(AnnoData.apply)(AnnoData.unapply)
  )

  val zScoreForm = Form(
    mapping(
      "missionName" -> text,
      "file" -> text,
    )(ZScoreData.apply)(ZScoreData.unapply)
  )

  val iPathForm = Form(
    mapping(
      "missionName" -> text,
      "file" -> text,
      "hasGroup" -> optional(text),
      "groupFile" -> text,
      "fcMethod" -> text,
    )(IPathData.apply)(IPathData.unapply)
  )

  val enrichForm = Form(
    mapping(
      "missionName" -> text,
      "file" -> text,
      "libType" -> text,
    )(EnrichData.apply)(EnrichData.unapply)
  )

  val parCorForm = Form(
    mapping(
      "missionName" -> text,
      "file" -> text,
      "hasExtraDataFile" -> boolean,
      "extraFile" -> text,
      "hasConfounderFile" -> boolean,
      "confounderFile" -> text,
      "corMethod" -> text,
      "fdrMethod" -> text,
      "coe" -> text,
      "p" -> text,
      "fdr" -> text,
    )(ParCorData.apply)(ParCorData.unapply)
  )

  val metMicCorForm = Form(
    mapping(
      "missionName" -> text,
      "file" -> text,
      "micFile" -> text,
      "hasCov" -> optional(text),
      "covFile" -> text,
      "metaNor" -> text,
      "rarefaction" -> text,
      "r" -> text,
      "alpha" -> text,
      "coe" -> text,
      "p" -> text,
    )(MetMicCorData.apply)(MetMicCorData.unapply)
  )

  val diagnoseForm = Form(
    mapping(
      "missionName" -> text,
      "file" -> text,
      "groupFile" -> text,
      "scaleMethod" -> text,
      "group" -> text,
      "method" -> text
    )(DiagnoseData.apply)(DiagnoseData.unapply)
  )

  val pcaForm = Form(
    mapping(
      "missionName" -> text,
      "file" -> text,
      "hasGroup" -> boolean,
      "groupFile" -> text,
      "scaleMethod" -> text,
      "isPaired" -> boolean,
    )(PcaData.apply)(PcaData.unapply)
  )

  val diffAnaForm = Form(
    mapping(
      "missionName" -> text,
      "file" -> text,
      "groupFile" -> text,
      "group" -> text,
      "isPaired" -> boolean,
      "method" -> text,
      "fcMethod" -> text,
      "pValue" -> text,
      "useFdr" -> boolean,
      "fdr" -> text,
      "log2FC" -> text,
    )(DiffAnaData.apply)(DiffAnaData.unapply)
  )

  val lefseAnaForm = Form(
    mapping(
      "missionName" -> text,
      "file" -> text,
      "groupFile" -> text,
      "level" -> text,
      "a" -> text,
      "w" -> text,
      "l" -> text,
      "y" -> text,
    )(LefseAnaData.apply)(LefseAnaData.unapply)
  )

  val limmaAnaForm = Form(
    mapping(
      "missionName" -> text,
      "file" -> text,
      "groupFile" -> text,
      "groups" -> list(text),
      "covs" -> list(text),
      "kinds" -> list(text),
      "fillMethod" -> text,
      "knn" -> text,
    )(LimmaAnaData.apply)(LimmaAnaData.unapply)
  )

  val diffAnaMulForm = Form(
    mapping(
      "missionName" -> text,
      "file" -> text,
      "groupFile" -> text,
      "method" -> text,
      "pValue" -> text,
      "useFdr" -> boolean,
      "fdr" -> text,
    )(DiffAnaMulData.apply)(DiffAnaMulData.unapply)
  )

  val ccaRdaForm = Form(
    mapping(
      "missionName" -> text,
      "file" -> text,
      "listFile" -> text,
      "analysisKind" -> text,
      "hasGroup" -> optional(text),
      "groupFile" -> text,
    )(CcaRdaData.apply)(CcaRdaData.unapply)
  )

  val heatmapForm = Form(
    mapping(
      "missionName" -> text,
      "file" -> text,
      "hasRowGroup" -> boolean,
      "rowGroupFile" -> text,
      "selectColumns" -> list(text),
      "hasColGroup" -> boolean,
      "colGroupFile" -> text,
    )(HeatmapData.apply)(HeatmapData.unapply)
  )

  val plsdaForm = Form(
    mapping(
      "missionName" -> text,
      "file" -> text,
      "groupFile" -> text,
      "scaleMethod" -> text,
      "isPaired" -> boolean,
    )(PlsdaData.apply)(PlsdaData.unapply)
  )

  val isPairedForm = Form(
    mapping(
      "file" -> text,
      "groupFile" -> text,
    )(IsPairedData.apply)(IsPairedData.unapply)
  )

  val groupForm = Form(
    mapping(
      "group" -> default(text, ""),
    )(GroupData.apply)(GroupData.unapply)
  )

  val svmForm = Form(
    mapping(
      "missionName" -> text,
      "file" -> text,
      "groupFile" -> text,
      "scaleMethod" -> text,
      "kernel" -> text,
      "cost" -> text,
      "tolerance" -> text,
      "epsilon" -> text,
      "gamma" -> text,
      "coef0" -> text,
      "degree" -> text,
      "topM" -> text,
    )(SvmData.apply)(SvmData.unapply)
  )

  val rfForm = Form(
    mapping(
      "missionName" -> text,
      "file" -> text,
      "groupFile" -> text,
      "scaleMethod" -> text,
      "ntree" -> text,
      "hasMtry" -> boolean,
      "mtry" -> text,
      "replace" -> text,
      "hasNodesize" -> boolean,
      "nodesize" -> text,
      "hasMaxnodes" -> boolean,
      "maxnodes" -> text,
      "topM" -> text,
    )(RfData.apply)(RfData.unapply)
  )

  val borutaForm = Form(
    mapping(
      "missionName" -> text,
      "file" -> text,
      "groupFile" -> text,
      "scaleMethod" -> text,
      "maxRuns" -> text,
      "pValue" -> text,
    )(BorutaData.apply)(BorutaData.unapply)
  )

  val biomarkerForm = Form(
    mapping(
      "missionName" -> text,
      "file" -> text,
      "diffFiles" -> list(text),
      "isInter" -> text
    )(BiomarkerData.apply)(BiomarkerData.unapply)
  )

  val vennForm = Form(
    mapping(
      "missionName" -> text,
      "labels" -> list(text),
      "diffFiles" -> list(text),
    )(VennData.apply)(VennData.unapply)
  )

  val basicForm = Form(
    mapping(
      "missionName" -> text,
      "file" -> text,
      "groupFile" -> text,
      "isPaired" -> optional(text),
      "method" -> text,
      "fcMethod" -> text,
      "mulMethod" -> text,
    )(BasicData.apply)(BasicData.unapply)
  )

  val oplsdaForm = Form(
    mapping(
      "missionName" -> text,
      "file" -> text,
      "groupFile" -> text,
      "scaleMethod" -> text,
      "group" -> text,
      "vip" -> text,
      "q2Y" -> text,
      "q2YB" -> text,
    )(OplsdaData.apply)(OplsdaData.unapply)
  )

  val detailForm = Form(
    mapping(
      "fileName" -> text,
      "kind" -> text
    )(DetailData.apply)(DetailData.unapply)
  )

  val metDataAddForm = Form(
    mapping(
      "inputKind" -> text,
      "fileContent" -> text,
      "varUniq" -> optional(text),
      "tags" -> text,
      "comment" -> text,
    )(MetDataAddData.apply)(MetDataAddData.unapply)
  )

  val groupDataAddForm = Form(
    mapping(
      "inputKind" -> text,
      "fileContent" -> text,
      "sampleHeader" -> optional(text),
      "classNoteHeader" -> optional(text),
      "pairHeader" -> optional(text),
      "tags" -> text,
      "comment" -> text,
    )(GroupDataAddData.apply)(GroupDataAddData.unapply)
  )

  val libDataAddForm = Form(
    mapping(
      "inputKind" -> text,
      "fileContent" -> text,
      "classHeader" -> optional(text),
      "hmdbHeader" -> optional(text),
      "keggHeader" -> optional(text),
      "metaboliteHeader" -> optional(text),
      "tags" -> text,
      "comment" -> text,
    )(LibDataAddData.apply)(LibDataAddData.unapply)
  )

  val exchangeForm = Form(
    mapping(
      "sampleHeader" -> text,
      "classNoteHeader" -> text,
      "pairHeader" -> optional(text),
    )(ExchangeData.apply)(ExchangeData.unapply)
  )

  val newGroupForm = Form(
    mapping(
      "fileName" -> text,
      "isPaired" -> boolean,
      "groupNames" -> list(text),
      "sampleNames" -> list(text),
    )(NewGroupData.apply)(NewGroupData.unapply)
  )

  val subForm = Form(
    mapping(
      "id" -> number,
      "fileName" -> text,
      "rows" -> list(text),
      "cols" -> list(text),
    )(SubData.apply)(SubData.unapply)
  )

  val hJoinForm = Form(
    mapping(
      "missionName" -> text,
      "inputKinds" -> seq(text),
      "fileContent" -> seq(text)
    )(HJoinData.apply)(HJoinData.unapply)
  )

  val fileNameForm = Form(
    mapping(
      "fileName" -> text
    )(FileNameData.apply)(FileNameData.unapply)
  )

  val updateForm = Form(
    mapping(
      "fileName" -> text,
      "tags" -> text,
      "comment" -> text,
    )(UpdateData.apply)(UpdateData.unapply)
  )

  val fileNamesForm = Form(
    mapping(
      "fileNames" -> list(text)
    )(FileNamesData.apply)(FileNamesData.unapply)
  )

  val nameForm = Form(
    mapping(
      "name" -> text
    )(NameData.apply)(NameData.unapply)
  )

  val fileNameOpForm = Form(
    mapping(
      "fileName" -> optional(text)
    )(FileNameOpData.apply)(FileNameOpData.unapply)
  )

  val columnNumLeastOpForm = Form(
    mapping(
      "columnNumLeast" -> optional(number)
    )(ColumnNumLeastOpData.apply)(ColumnNumLeastOpData.unapply)
  )

  val originalFileNameOpForm = Form(
    mapping(
      "originalFileName" -> optional(text)
    )(OriginalFileNameOpData.apply)(OriginalFileNameOpData.unapply)
  )

  val originalFileNameForm = Form(
    mapping(
      "originalFileName" -> text
    )(OriginalFileNameData.apply)(OriginalFileNameData.unapply)
  )

  val groupNumOpForm = Form(
    mapping(
      "groupNum" -> optional(number)
    )(GroupNumOpData.apply)(GroupNumOpData.unapply)
  )

  val groupKindForm = Form(
    mapping(
      "groupKind" -> default(text, "col")
    )(GroupKindData.apply)(GroupKindData.unapply)
  )

  val onlyInterForm = Form(
    mapping(
      "onlyInter" -> default(boolean, false)
    )(OnlyInterData.apply)(OnlyInterData.unapply)
  )

  val sampleIdForm = Form(
    mapping(
      "id" -> text
    )(SampleIdData.apply)(SampleIdData.unapply)
  )

  val sampleSummaryForm = Form(
    mapping(
      "id" -> text,
      "panel" -> text
    )(SampleSummaryData.apply)(SampleSummaryData.unapply)
  )

  val kindForm = Form(
    mapping(
      "kind" -> text
    )(KindData.apply)(KindData.unapply)
  )

  val extraDataForm = Form(
    mapping(
      "prefix" -> default(text, "")
    )(ExtraDataData.apply)(ExtraDataData.unapply)
  )

  val softKindForm = Form(
    mapping(
      "softKind" -> text
    )(SoftKindData.apply)(SoftKindData.unapply)
  )

  val softKindOpForm = Form(
    mapping(
      "softKind" -> optional(text)
    )(SoftKindOpData.apply)(SoftKindOpData.unapply)
  )

  val kindOpForm = Form(
    mapping(
      "kind" -> optional(text)
    )(KindOpData.apply)(KindOpData.unapply)
  )

  val missionNameForm = Form(
    mapping(
      "missionName" -> text
    )(MissionNameData.apply)(MissionNameData.unapply)
  )

  val idForm = Form(
    mapping(
      "id" -> number
    )(IdData.apply)(IdData.unapply)
  )

  val idStrForm = Form(
    mapping(
      "idStr" -> text
    )(IdStrData.apply)(IdStrData.unapply)
  )

  val idStrOpForm = Form(
    mapping(
      "idStr" -> optional(text)
    )(IdStrOpData.apply)(IdStrOpData.unapply)
  )

  val idOpForm = Form(
    mapping(
      "id" -> optional(number)
    )(IdOpData.apply)(IdOpData.unapply)
  )

  val idsForm = Form(
    mapping(
      "ids" -> list(number)
    )(IdsData.apply)(IdsData.unapply)
  )

  val idStrsForm = Form(
    mapping(
      "idStrs" -> list(text)
    )(IdStrsData.apply)(IdStrsData.unapply)
  )

  val exampleForm = Form(
    mapping(
      "kind" -> text,
      "fileName" -> text
    )(ExampleData.apply)(ExampleData.unapply)
  )


  val stepForm = Form(
    mapping(
      "missionName" -> text,
      "stepNum" -> number,
      "id" -> text,
      "kitName" -> text
    )(StepData.apply)(StepData.unapply)
  )

  val batchMissionForm = Form(
    mapping(
      "missionIntro" -> text,
      "dataType" -> text,
      "splitDir" -> text,
      "serverGzFile" -> text,
      "isCover" -> optional(boolean)
    )(BatchMissionData.apply)(BatchMissionData.unapply)
  )

  val serverDirForm = Form(
    mapping(
      "splitDir" -> text
    )(ServerDirData.apply)(ServerDirData.unapply)
  )

  val adminSetForm = Form(
    mapping(
      "gzPath" -> text,
    )(AdminSetData.apply)(AdminSetData.unapply)
  )

  val nodeForm = Form(
    mapping(
      "id" -> text,
    )(NodeData.apply)(NodeData.unapply)
  )

  val missionIdForm = Form(
    mapping(
      "missionId" -> number
    )(MissionIdData.apply)(MissionIdData.unapply)
  )

  val siteNamesForm = Form(
    mapping(
      "siteNames" -> seq(text)
    )(SiteNamesData.apply)(SiteNamesData.unapply)
  )

  val columnNamesForm = Form(
    mapping(
      "columnNames" -> seq(text)
    )(ColumnNamesData.apply)(ColumnNamesData.unapply)
  )

  val sexForm = Form(
    mapping(
      "sex" -> text
    )(SexData.apply)(SexData.unapply)
  )

  val compareMissionForm = Form(
    mapping(
      "userId" -> number,
      "missionName" -> text,
      "sample1" -> text,
      "sample2" -> text
    )(CompareMissionData.apply)(CompareMissionData.unapply)
  )

  val kitNameForm = Form(
    mapping(
      "kitName" -> text
    )(KitNameData.apply)(KitNameData.unapply)
  )

  val sampleIdsForm = Form(
    mapping(
      "ids" -> seq(text)
    )(SampleIdsData.apply)(SampleIdsData.unapply)
  )

  val pageForm = Form(
    mapping(
      "limit" -> number,
      "offset" -> number,
      "order" -> text,
      "search" -> optional(text),
      "sort" -> optional(text),
      "gender" -> optional(list(text)),
      "kind" -> optional(list(text)),
      "panel" -> optional(list(text)),
      "id" -> optional(text),
      "indexId" -> optional(text),
      "sampleIntro" -> optional(text),
      "comment" -> optional(text),
      "updateMission" -> optional(list(text)),
      "startTime" -> optional(jodaDate),
      "endTime" -> optional(jodaDate),
      "containerName" -> optional(list(text)),
      "singleSource" -> optional(list(text)),
      "autosomalLociTyped" -> optional(list(text)),
      "yLociTyped" -> optional(list(text)),
      "xLociTyped" -> optional(list(text)),
      "isnpLociTyped" -> optional(list(text)),
      "interlocusBalance" -> optional(list(text))
    )(PageData.apply)(PageData.unapply)
  )

  val phoneForm = Form(
    mapping(
      "phone" -> text
    )(PhoneData.apply)(PhoneData.unapply)
  )

  val newPhoneForm = Form(
    mapping(
      "newPhone" -> text
    )(NewPhoneData.apply)(NewPhoneData.unapply)
  )

  val emailForm = Form(
    mapping(
      "email" -> text
    )(EmailData.apply)(EmailData.unapply)
  )

  val registerUserForm = {
    Form(
      mapping(
        "fullName" -> text,
        "password" -> text,
        "email" -> text,
        "phone" -> default(text, ""),
        "company" -> text,
        "province" -> default(text, ""),
        "city" -> default(text, ""),
        "teacherName" -> default(text, "")
      )(RegisterUserData.apply)(RegisterUserData.unapply)
    )
  }

  val updateUserForm = Form(
    mapping(
      "fullName" -> text,
      "email" -> text,
      "phone" -> default(text, ""),
      "institution" -> text,
      "province" -> default(text, ""),
      "city" -> default(text, ""),
      "teacher" -> default(text, "")
    )(UpdateUserData.apply)(UpdateUserData.unapply)
  )

  val validCodeForm = Form(
    mapping(
      "phone" -> text,
      "validCode" -> text
    )(ValidCodeData.apply)(ValidCodeData.unapply)
  )

  val validEmailCodeForm = Form(
    mapping(
      "email" -> text,
      "validCode" -> text
    )(ValidEmailCodeData.apply)(ValidEmailCodeData.unapply)
  )

  val newPasswordForm = Form(
    mapping(
      "phone" -> text,
      "password" -> text
    )(NewPasswordData.apply)(NewPasswordData.unapply)
  )

  val columnNameForm = Form(
    mapping(
      "columnName" -> text
    )(ColumnNameData.apply)(ColumnNameData.unapply)
  )

  val changePasswordForm = Form(
    mapping(
      "password" -> text,
      "newPassword" -> text
    )(ChangePasswordData.apply)(ChangePasswordData.unapply)
  )


}
